﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema
{
    public partial class QLGhe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsG();
        }
        public void LaydsG()
        {
            DataTable ds_g;
            string mg = "";
            ds_g = LietKeGhe_Ma(mg);
            for (int i = 0; i < ds_g.Rows.Count; i++)
            {
                string str = ds_g.Rows[i]["mag"].ToString();
                DDLMAG.Items.Add(str);
            }
            DDLTinhTrang.Items.Add("Tốt");
            DDLTinhTrang.Items.Add("Hư");
            grGhe.DataSource = ds_g;
            grGhe.DataBind();
        }
        public DataTable LietKeGhe_Ma(string MaG)
        {
            try
            {
                if(MaG != null || MaG != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                     sqlPara.Add(new SqlParameter("@mag", MaG));
                    return CDatastore.DocStore("SP_TIMKIEM_GHE_MAG",sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_GHE_MAG");
        }

        public DataTable LietKeGhe_TinhTrang(int tinhtrang)
        {
            try
            {
                if (tinhtrang != -1 )
                {
                     List<SqlParameter> sqlPara = new List<SqlParameter>();
                     
                     sqlPara.Add(new SqlParameter("@tinhtrang", tinhtrang));
                    return CDatastore.DocStore("SP_TIMKIEM_GHE_TINHTRANG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text=e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_GHE_MAG");
        }

        protected void DDLMAG_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            DataTable dt = LietKeGhe_Ma(DDLMAG.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtSoHang.Text = dt.Rows[0][1].ToString();
                txtSoDay.Text = dt.Rows[0][2].ToString();
                txtMaPhong.Text = dt.Rows[0][3].ToString();
                txtMaLG.Text = dt.Rows[0][4].ToString();
                if (dt.Rows[0][5].ToString() == "True")
                    DDLTinhTrang.Text= "Tốt";
                else
                    DDLTinhTrang.Text = "Hư";
            }
        }

        protected void grGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void btCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@mag", DDLMAG.Text.ToString()));
                if (DDLTinhTrang.Text == "Hư")
                    sqlPara.Add(new SqlParameter("@tinhtrang", '0'));
                else
                    sqlPara.Add(new SqlParameter("@tinhtrang", '1'));
                CDatastore.ThucHienStore("SP_CAPNHATTINHTRANG_GHE", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Ghế " + DDLMAG.Text + " thành công";
                DDLMAG.Items.Clear();
                DDLTinhTrang.Items.Clear();
                LaydsG();
                

            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            int i = -1;
            //string tt = DDLMAG.Text;
            if (DDLTinhTrang.Text == "Hư")
                i = 0;
            else
                i = 1;
            DataTable dt = LietKeGhe_TinhTrang(i);
           
            grGhe.DataSource = dt;
      
            grGhe.DataBind();
        }

        protected void grGhe_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grGhe.PageIndex = e.NewPageIndex;
            grGhe.DataBind();
            LaydsG();
        }
    }
}